package com.stu.controller;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stu.common.R;
import com.stu.mapper.AccountFlowMapper;
import com.stu.pojo.Account;
import com.stu.pojo.AccountFlow;
import com.stu.pojo.Student;
import com.stu.pojo.vo.AccountVo;
import com.stu.pojo.vo.StudentDescVo;
import com.stu.pojo.vo.StudentVo1;
import com.stu.service.IAccountFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2023-12-20
 */
@RestController
@RequestMapping("/accountFlow")
public class AccountFlowController {
    @Autowired
    IAccountFlowService accountFlowService;

    //导出流水信息
    @GetMapping("/output")
    public R output(@RequestParam Set<String> includeColumnFiledNames,
                    String studentName,String accountName,
                    HttpServletResponse response){
        System.out.println(includeColumnFiledNames);
        response.setHeader("Content-Disposition","attachment;filename="+ UUID.randomUUID()+"stuoa.xlsx");
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        LambdaQueryWrapper<AccountFlow> qw = new LambdaQueryWrapper<>();
        qw.like(!StringUtils.isEmpty(studentName),AccountFlow::getStudentName,studentName);//学生名
        qw.like(!StringUtils.isEmpty(accountName),AccountFlow::getAccountName,accountName);//账户名
        qw.orderByDesc(AccountFlow::getUpdateTime);
        List<AccountFlow> list = accountFlowService.list(qw);
        int i = new Random().nextInt();
        EasyExcel.write(out, AccountFlow.class).includeColumnFiledNames(includeColumnFiledNames).sheet("模板")
                .doWrite(list);
        return R.success(list);
    }

    @GetMapping("/page")
    public R page(Integer page, Integer pageSize, String name,String accountName){
        Page<AccountFlow> userPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<AccountFlow> qw = new LambdaQueryWrapper<>();
        qw.like(!StringUtils.isEmpty(name),AccountFlow::getStudentName,name);//学生名
        qw.like(!StringUtils.isEmpty(accountName),AccountFlow::getAccountName,accountName);//账户名
        qw.orderByDesc(AccountFlow::getUpdateTime);
        accountFlowService.page(userPage,qw);
        return R.success(userPage);
    }

    //查看该学生的缴费情况
    @GetMapping("/student/{id}")
    public R selectFlowByStudent(@PathVariable Long id){
        LambdaQueryWrapper<AccountFlow> qw = new LambdaQueryWrapper<>();
        qw.like(AccountFlow::getStudentId,id);//学生名
        qw.orderByDesc(AccountFlow::getUpdateTime);
        List<AccountFlow> list = accountFlowService.list(qw);
        return R.success(list);
    }
}

